package com.lemon.exam.common.properties;

import com.lemon.exam.common.constant.ApiConst;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * 系统配置文件
 *
 * @author Lemon
 * @since 2025/2/18 17:43
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "config")
public class ConfigProperties {
    // 系统白名单
    private List<String> systemIgnoreUri;
    // 文件上传配置
    private FileUpload fileUpload;
    // knife4j白名单配置
    private List<String> knife4jIgnoreUri;
    // knife4j静态资源配置
    private Map<String, String> knife4jStaticResources;
    // rsa密钥配置
    private Rsa rsa;
    // http配置
    private HttpAllowed allowed;
    // jwt配置
    private Jwt jwt;
    // 代码生成器配置
    private Generator generator;

    public String[] getKnife4jIgnoreUriArray() {
        return knife4jIgnoreUri.toArray(ApiConst.EMPTY_STRING_ARRAY);
    }

    public String[] getSystemIgnoreUriArray() {
        return systemIgnoreUri.toArray(ApiConst.EMPTY_STRING_ARRAY);
    }

    public List<String> getAllIgnoreUri() {
        return Stream.concat(knife4jIgnoreUri.stream(), systemIgnoreUri.stream()).toList();
    }

    /**
     * 获取上传路径
     *
     * @param path
     * @return
     */
    public String getUploadPath(String path) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        return this.getFileUpload().getResource() + path;
    }

    @Getter
    @Setter
    public static class FileUpload {
        // 匹配路径
        private String match;
        // 存储路径
        private String resource;

        public String getLocationPath() {
            return "file:" + resource;
        }
    }

    @Getter
    @Setter
    public static class Rsa {
        // 公钥
        private String publicKey;
        // 私钥
        private String privateKey;
    }

    @Getter
    @Setter
    public static class HttpAllowed {
        // 允许的方法
        private List<String> methods;
        // 允许的域
        private List<String> origins;
        // 拦截的url
        private String mapping;
        // 允许的头
        private List<String> headers;
        // 允许凭证
        private Boolean credential;

        public String[] getMethodsArray() {
            return methods.toArray(ApiConst.EMPTY_STRING_ARRAY);
        }

        public String[] getOriginsArray() {
            return origins.toArray(ApiConst.EMPTY_STRING_ARRAY);
        }

        public String[] getHeadersArray() {
            return headers.toArray(ApiConst.EMPTY_STRING_ARRAY);
        }
    }

    @Getter
    @Setter
    public static class Jwt {
        // jwt加密密钥
        private String secret;
        // 令牌有效期
        private Long accessTokenExpirationMinutes;
        // 刷新令牌有效期
        private Long refreshTokenExpirationMinutes;
        // 刷新令牌请求头
        private String refreshTokenHeader;
        // 令牌前缀
        private String accessTokenPrefix;
        // 令牌前缀
        private String refreshTokenPrefix;
        // 登录路径
        private String loginPath;
        // 登出路径
        private String logoutPath;
        // 刷新路径
        private String refreshPath;
    }

    @Getter
    @Setter
    public static class Generator {
        //作者
        private String author;
        //是否覆盖原文件
        private Boolean fileOverride;
        //生成的web文件位置
        private String webPath;
    }
}
